<!-- 左右滑动布局 -->
<script lang="ts" setup>
import { ISlideLayoutLF } from './ISlideLayoutLF';

onMounted(() => {
  dragControllerDiv();
});

const props = defineProps({
  config: {
    type: Object,
    default: (): ISlideLayoutLF => ({
      leftMinWidth: 60,
      rightMinWidth: 60,
      mouseDownColor: '#818181',
      mouseUpColor: '#d6d6d6',
      leftWidthInit: '67%',
      rightWidthInit: '32%'
    })
  }
});

const { config } = toRefs(props);

function dragControllerDiv() {
  const resize: any = document.getElementsByClassName('resize');
  const left: any = document.getElementsByClassName('left');
  const mid: any = document.getElementsByClassName('mid');
  const box: any = document.getElementsByClassName('box');
  for (let i = 0; i < resize.length; i++) {
    // 鼠标按下事件
    resize[i].onmousedown = function (e: MouseEvent) {
      //颜色改变提醒
      resize[i].style.background = config.value.mouseDownColor;
      const startX = e.clientX;
      resize[i].left = resize[i].offsetLeft;
      // 鼠标拖动事件
      document.onmousemove = function (e) {
        const endX = e.clientX;
        let moveLen = resize[i].left + (endX - startX); // （endx-startx）=移动的距离。resize[i].left+移动的距离=左边区域最后的宽度
        const maxT = box[i].clientWidth - resize[i].offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度

        if (moveLen < config.value.leftMinWidth) moveLen = config.value.leftMinWidth; // 左边区域的最小宽度
        if (moveLen > maxT - config.value.rightMinWidth)
          moveLen = maxT - config.value.rightMinWidth; //右边区域最小宽度

        resize[i].style.left = moveLen; // 设置左侧区域的宽度

        for (let j = 0; j < left.length; j++) {
          left[j].style.width = moveLen + 'px';
          mid[j].style.width = box[i].clientWidth - moveLen - 10 + 'px';
        }
      };
      // 鼠标松开事件
      // @ts-ignore
      document.onmouseup = function (evt: Event) {
        //颜色恢复
        resize[i].style.background = config.value.mouseUpColor;
        document.onmousemove = null;
        document.onmouseup = null;
        resize[i].releaseCapture && resize[i].releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
      };
      resize[i].setCapture && resize[i].setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获
      return false;
    };
  }
}

// 计算右侧容器宽度
const midWidth = computed(() => {
  const leftWidth = parseFloat(config.value.leftWidthInit);
  return `calc(${leftWidth}% - 20px)`;
});
</script>

<template>
  <el-container class="box" style="height: 100vh">
    <el-aside :width="midWidth" class="left">
      <el-scrollbar>
        <slot name="left"></slot>
      </el-scrollbar>
    </el-aside>
    <div class="resize"></div>
    <el-container class="mid" :style="{ width: midWidth }">
      <el-main>
        <el-scrollbar>
          <slot name="right" :style="{ width: config.rightWidthInit }"></slot>
        </el-scrollbar>
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.layout-container-demo .el-header {
  position: relative;
  background-color: var(--el-color-primary-light-7);
  color: var(--el-text-color-primary);
}

.layout-container-demo .el-aside {
  color: var(--el-text-color-primary);
  background: var(--el-color-primary-light-8);
}

.layout-container-demo .el-menu {
  border-right: none;
}

.layout-container-demo .el-main {
  padding: 0;
}

.layout-container-demo .toolbar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  right: 20px;
}

/* 拖拽相关样式 */
/*包围div样式*/
.box {
  width: 100%;
  height: 100%;
  overflow: hidden;
  box-shadow: -1px 9px 10px 3px rgba(0, 0, 0, 0.11);
}

/*左侧div样式
  //width: calc(32% - 10px); /*左侧初始化宽度
*/
.left {
  height: 100%;
  background: #ffffff;
  float: left;
  box-shadow: 1px 4px 5px 3px rgba(0, 0, 0, 0.11);
}

/*拖拽区div样式*/
.resize {
  cursor: col-resize;
  float: left;
  position: relative;
  top: 25%;
  background-color: #d6d6d6;
  border-radius: 5px;
  margin-top: -10px;
  width: 6px;
  height: 50vh;
  background-size: cover;
  background-position: center;
  /*z-index: 99999;*/
  font-size: 32px;
  color: white;
}

/*拖拽区鼠标悬停样式*/
.resize:hover {
  color: #444444;
}

/*右侧div'样式*/
.mid {
  float: left;
  height: 100%;
  background: #fff;
  box-shadow: -1px 4px 5px 3px rgba(0, 0, 0, 0.11);
}
</style>
